package com.dcivision.framework.web;

/*
 * @(#)PreservingUrlFilter.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

/**
 * This filter controls the preservation of the URI/URL of the HttpReqeust originated from the client
 * 
 * @author Ferdinand Chan
 * @company DCIVision Limited
 * @creation date 09/11/2005
 * @version $Revision: 1.2 $
 */
public class PreservingUrlFilter implements Filter {

	//The configuration of this Filter
	private FilterConfig config;
	
	public void init(FilterConfig config) throws ServletException {
		//Save a reference to the configuration of this filter
		this.config = config;
	}

	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain chain) throws IOException, ServletException {

		//Obtain the flags from the config		
		boolean preserveUri = false;
		boolean preserveUrl = false;
		
		if(config.getInitParameter("preserveURI") != null && config.getInitParameter("preserveURI").equals("true"))
			preserveUri = true;
		
		if(config.getInitParameter("preserveURL") != null && config.getInitParameter("preserveURL").equals("true"))
			preserveUrl = true;
			
		//Wrap the request with the PreservingUrlHttpServletRequestWrapper
		PreservingUrlHttpServletRequestWrapper wrapper = 
			new PreservingUrlHttpServletRequestWrapper((HttpServletRequest)req,preserveUri,preserveUrl);
		
		//Carry on to other filters
		chain.doFilter(wrapper,res);
	}

	public void destroy() {
		//Release the reference to the config
		config = null;
	}

}
